package models

import (
	"gintest/database"
	"time"
)

//表结构 用于管理接口输出
type Order struct {
	Id        int    `json:"id" gorm:"column:id;primary_key" form:"id"`
	UserId  int `json:"user_id" gorm:"column:user_id" form:"user_id"`
	StoreId  int `json:"store_id" gorm:"column:store_id" form:"store_id"`
	OrderNum string `json:"order_num" gorm:"column:order_num" form:"order_num"`
	LogisticsId int `json:"logistics_id" gorm:"column:logistics_id" form:"logistics_id"`
	LogisticsNum string `json:"logistics_num" gorm:"column:logistics_num" form:"logistics_num"`
	Money float64 `json:"money" gorm:"column:money" form:"money"`
	Status int `json:"status" gorm:"column:status" form:"status"`
	Pay int `json:"pay" gorm:"column:pay" form:"pay"`
	Remark string `json:"remark" gorm:"column:remark" form:"remark"`
	CreateAt time.Time  `json:"create_at" gorm:"column:create_at;type:datetime" time_format:"2006-01-02 15:04:05" form:"create_at"`
	UpdateAt time.Time  `json:"update_at" gorm:"column:update_at;type:datetime" time_format:"2006-01-02 15:04:05" form:"update_at"`
	DeleteAt time.Time  `json:"delete_at" gorm:"column:delete_at;type:datetime" time_format:"2006-01-02 15:04:05" form:"delete_at"`

	OrderDelivery OrderDelivery `json:"order_delivery" gorm:"ForeignKey:OrderId;AssociationForeignKey:Id"`
	Logistics Logistics `json:"logistics" gorm:"ForeignKey:LogisticsId;AssociationForeignKey:Id"`

	OrderItem []OrderItem `json:"order_item" gorm:"ForeignKey:OrderId;AssociationForeignKey:Id"`
}
func (Order) TableName() string {
	return "order"
}
func GetOrderList(page uint64,pagesize uint64,status,pay uint64,start,end,keyword,area string)(data []Order,total uint64){
	var offset=(page-1)*pagesize
	db:=database.SqlDB.Model(Order{})
	if status!=0 {
		db=db.Where("order.status=?",status)
	}
	if pay!=0{
		db=db.Where("order.pay=?",pay)
	}
	if len(area)!=0{
		db=db.Where("concat(order_delivery.province,order_delivery.city) like ?","%"+area+"%")
	}
	if len(keyword)!=0{
		db=db.Where("concat(order.order_num,order_delivery.name,order_delivery.phone) like ?","%"+keyword+"%")
	}
	if len(start)!=0 && len(end)!=0{
		db=db.Where("order.create_at >= ? and order.create_at <= ?",start,end)
	}else if len(start)!=0 && len(end)==0{
		db=db.Where("order.create_at >= ?",start)
	}else if len(start)==0 && len(end)!=0{
		db=db.Where("order.create_at <= ?",end)
	}

    db=db.Where("order.delete_at = 0 or order.delete_at is null")
	db.Joins("left join order_delivery on order.id = order_delivery.order_id").Preload("OrderDelivery","delete_at = 0 or delete_at is null").Preload("Logistics").Offset(offset).Limit(pagesize).Order("order.update_at desc").Find(&data)
	db.Model(Order{}).Count(&total)

	return data,total
}
func GetOrderListOfStore(page uint64,pagesize uint64,store_id,status,user_id int)(data []Order){
	var offset=(page-1)*pagesize
	db:=database.SqlDB.Model(Order{})
	if status!=0 {
		db=db.Where("status=?",status)
	}
	db=db.Where("user_id=?",user_id)
    db=db.Where("delete_at = 0 or delete_at is null and store_id=?",store_id)
	db.Offset(offset).Limit(pagesize).Order("update_at desc").Find(&data)

	return data
}
func AddOrder(data *Order) error{
	if data.UpdateAt.IsZero(){
		data.UpdateAt=time.Now()
	}
	if data.CreateAt.IsZero(){
		data.CreateAt=time.Now()
	}
	return database.SqlDB.Model(Order{}).Omit("Logistics").Create(data).Error
}
func GetOrderById(id int) (data Order){
	database.SqlDB.Model(Order{}).Preload("OrderDelivery","delete_at = 0 or delete_at is null").Preload("Logistics").Preload("OrderItem","delete_at = 0 or delete_at is null").Where("id=?",id).First(&data);
	return data
}
func DelOrderByIds(ids []int) error{
	tx:=database.SqlDB.Begin()
	err:=tx.Model(OrderDelivery{}).Where("order_id in (?) and (delete_at = 0 or delete_at is null)",ids).Update("delete_at",time.Now().Local().String()).Error
	if err!=nil{
		tx.Rollback()
		return err
	}
	err=tx.Model(OrderItem{}).Where("order_id in (?) and (delete_at = 0 or delete_at is null)",ids).Update("delete_at",time.Now().Local().String()).Error
	if err!=nil{
		tx.Rollback()
		return err
	}
	err=tx.Model(Order{}).Where("id in (?) and (delete_at = 0 or delete_at is null)",ids).Update("delete_at",time.Now().Local().String()).Error
	if err!=nil{
		tx.Rollback()
		return err
	}
	tx.Commit()
	return nil

}
